package com.neuedu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neuedu.mapper.TElderMapper;
import com.neuedu.pojo.Result;
import com.neuedu.pojo.TElder;
import com.neuedu.pojo.TRecord;
import com.neuedu.mapper.TRecordMapper;
import com.neuedu.pojo.dto.nurse.NurseInfoDTO;
import com.neuedu.pojo.dto.record.CheckIOPageDTO;
import com.neuedu.pojo.dto.record.CheckIOPageResult;
import com.neuedu.service.ITRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhangsan
 * @since 2024-06-17
 */
@Service
public class TRecordServiceImpl extends ServiceImpl<TRecordMapper, TRecord> implements ITRecordService {

    @Autowired
    private TElderMapper elderMapper;
    @Override
    public Result<List<CheckIOPageResult>> getPage(CheckIOPageDTO dto) {
        List<TElder> tElders = elderMapper.selectList(new LambdaQueryWrapper<TElder>()
                .like(StringUtils.isNotBlank(dto.getElderName()), TElder::getEldName, dto.getElderName()));
        List<Long> collect = tElders.stream().map(TElder::getEldId).collect(Collectors.toList());
        Page<TRecord> page = new Page<>(dto.getCurrentPage(), dto.getPageSize());
        LambdaUpdateWrapper<TRecord> queryWrapper = new LambdaUpdateWrapper<TRecord>()
                .in(TRecord::getElderId, collect)
                .eq(TRecord::getTypeofrecord, dto.getTypeofrecord())
                .eq(dto.getApprovalState() != null, TRecord::getRecordState, dto.getApprovalState());
        if (dto.getApprovalTime() != null) {
            queryWrapper.ge(TRecord::getRecordTime, dto.getApprovalTime())
                    .lt(TRecord::getRecordTime, dto.getApprovalTime().plusDays(1L));
        }
        if (dto.getSubmissionTime() != null) {
            queryWrapper.ge(TRecord::getUpdateTime, dto.getSubmissionTime())
                    .lt(TRecord::getUpdateTime,dto.getSubmissionTime().plusDays(1L));
        }
        baseMapper.selectPage(page, queryWrapper);
        List<TRecord> records = page.getRecords();
        List<CheckIOPageResult> checkIOPageResults = records.stream().map(tRecord -> {
            CheckIOPageResult checkIOPageResult = new CheckIOPageResult();
            BeanUtils.copyProperties(tRecord, checkIOPageResult);
            TElder tElder = elderMapper.selectById(tRecord.getElderId());
            checkIOPageResult.setEldId(tElder.getEldId());
            checkIOPageResult.setElderName(tElder.getEldName());
            checkIOPageResult.setEldIdNum(tElder.getEldIdNum());
            switch (tRecord.getRecordState()){
                case 0:
                    checkIOPageResult.setStatus("待审批");
                    break;
                case 1:
                    checkIOPageResult.setStatus("未通过");
                    break;
                case 2:
                    checkIOPageResult.setStatus("已通过");
                    break;
            }
            return checkIOPageResult;
        }).collect(Collectors.toList());
        return Result.success(checkIOPageResults, "查询成功", page.getTotal());
    }
}
